<?php

namespace app\index\controller;

use think\facade\Db;
use think\admin\helper\ValidateHelper;

class Company
{
    public function index(){
        return view();
    }

    public function detail()
    {
    	$data = $this->_vali([
            'id.default' => '',
        ], "get");

    	$info = Db::name('member')->where('id',$data['id'])->find();

    	$info['address'] = Db::name('address')->where('id',$info['address'])->value('name');

        $info['classification'] = Db::name('industry')->where('id',$info['classification'])->value('name');


        return view('',['info'=>$info]);
    }

    /**
     * 获取地区
     * @return [type] [description]
     */
    public function address()
    {
    	$list = Db::name('address')->field('id,name')->select();

    	return json(['status'=>200,'list'=>$list]);
    }

    /**
     * 获取行业
     * @return [type] [description]
     */
    public function industry()
    {
    	$list = Db::name('industry')->field('id,name')->select();

    	return json(['status'=>200,'list'=>$list]);
    }

    /**
     * 获取列表
     * @return [type] [description]
     */
    public function list()
    {
    	$data = $this->_vali([
            'keyword.default' => '',
            'address.default' => '',
            'classification.default' => '',
        ], "POST");

        $where = [];

        if ($data['keyword']) {
            $where[] = ['name', 'like', "%" . $data['keyword'] . "%"];
        }
        if ($data['address']) {
            $where[] = ['address','=', $data['address']];
        }
        if ($data['classification']) {
            $where[] = ['classification','=', $data['classification']];
        }

        $list = Db::name('member')->where($where)->paginate(10);

        $list = $list->toArray();

        foreach ($list['data'] as $key => $value) {
        	$list['data'][$key]['address'] = Db::name('address')->where('id',$value['address'])->value('name');

        	$list['data'][$key]['classification'] = Db::name('industry')->where('id',$value['classification'])->value('name');
        }

        return json(['status' => 200, 'sum' => $list['total'], 'list' => $list['data']]);
    }

    /**
     * 快捷输入并验证（ 支持 规则 # 别名 ）
     * @param array $rules 验证规则（ 验证信息数组 ）
     * @param string|array $type 输入方式 ( post. 或 get. )
     * @param callable|null $callable 异常处理操作
     * @return array
     */
    protected function _vali(array $rules, $type = '', ?callable $callable = null): array
    {
        return ValidateHelper::instance()->init($rules, $type, $callable);
    }
}